﻿using AutoMapper;
using MediatR;
using Provenance.Domain.Breed;
using Provenance.ErrorCount;
using Provenance.Infra.Interfaces.Breed;
using Provenance.Read.Api.Application.Command.Breed;

namespace Provenance.Read.Api.Application.CommandHandler.Breed
{
    public class BackBreedHealthCommandHandler : IRequestHandler<BackBreedHealthCommand, ApiResult<BreedHealth>>
    {
        private readonly IBreedHealthRepository _breedHealthRepository;
        private readonly ILogger<BackBreedHealthCommandHandler> _logger;
        private readonly IMapper _mapper;
        public BackBreedHealthCommandHandler(IBreedHealthRepository breedHealthRepository, ILogger<BackBreedHealthCommandHandler> logger, IMapper mapper)
        {
            _breedHealthRepository = breedHealthRepository;
            _logger = logger;
            _mapper = mapper;
        }
        public async Task<ApiResult<BreedHealth>> Handle(BackBreedHealthCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var obj = await _breedHealthRepository.GetAsync(x => x.ProductInfoId == request.ProductInfoId&&!x.IsDeleted);
                return ApiResult<BreedHealth>.Success(obj);
            }
            catch (Exception ex)
            {
                _logger.LogError("Error getting breed health: " + ex.Message);
                return ApiResult<BreedHealth>.Failure(ex.Message);
            }
        }
    }
}
